//Created by Amit Mudgal (MicroStrategy Prinipal Consultant) for American Express GMS group on 4/29/2014

package com.AmericanExpress.LookandFeel.addons;

import com.microstrategy.web.app.WebAppRuntimeException;
import com.microstrategy.web.app.addons.AbstractAppAddOn;
import com.microstrategy.web.app.beans.PageComponent;
import com.microstrategy.web.beans.AggregatedWebFeatures;
import com.microstrategy.web.beans.WebComponent;


public class AggregatedFeaturesAddOn extends AbstractAppAddOn { 
    private String _className;
    private String _targetBean;

    public AggregatedFeaturesAddOn() {
        super();
        _className = null;
        _targetBean = null;
    }
    public void preCollectData(PageComponent page) {
        WebComponent bean; //Target bean

        //This Add-on requires the fully qualified class name of the custom webfeatures.
        if (_className == null) {
            throw new WebAppRuntimeException("The AggregatedFeaturesAddOn requires that the customFeaturesClassName property is set before calling CollectData on the page");
        }

        //Chose the targetBean, if none specified, use the page.
        if (_targetBean == null) {
            bean = page;
        } else {
            bean = page.searchChild(_targetBean);

            if (bean == null) {
                throw new WebAppRuntimeException("The web-bean " + _targetBean + " was not found. Please make sure the bean name is correct.");
            }
        }

        //Create the custom AggregatedWebFeatures using reflection,
        // and call the aggregateFeatures method with the target-bean.
        try {
            AggregatedWebFeatures customFeatures = (AggregatedWebFeatures) Class.forName(_className).newInstance();
            customFeatures.aggregateFeatures(bean);

        } catch (ClassNotFoundException ex) {
            throw new WebAppRuntimeException("The class " + _className + " was not found. Please verify the value is the fully qualified class name of your custom features.");
        } catch (IllegalAccessException ex) {
            throw new WebAppRuntimeException("Could not create an instance of " + _className + ". Please verify the value is the fully qualified class name of your custom features.");
        } catch (InstantiationException ex) {
            throw new WebAppRuntimeException("Could not create an instance of " + _className + ". Please verify the value is the fully qualified class name of your custom features.");
        } catch (ClassCastException ex) {
            throw new WebAppRuntimeException("The custom WebFeatures class " + _className + " needs to extend  AggregatedWebFeatures in order to use this add-on.");
        }

    }
    public void setCustomFeaturesClassName(String fqcn) {
        _className = fqcn;
    }

    public void setTargetBean(String beanName) {
        _targetBean = beanName;
    }
    public String getAddOnDescription() {
        return "This AddOn can be used to associate a custom AggregatedWebFeatures class to a bean";
    }

}